package com.quzi.kettle.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.quzi.kettle.R;
import com.quzi.kettle.bean.ModeItem;

public class ExpandableAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<ModeItem> list;
	private IExpandableAdapter callback;

	private String[] generals = new String[] { "自定义", "奶粉", "茶类", "豆乳", "咖啡"

	};
	private int[] logos = new int[] { R.drawable.custom, R.drawable.baby_milk,
			R.drawable.greentea, R.drawable.redtea, R.drawable.coffeetea };

	public ExpandableAdapter(Context context, List<ModeItem> items) {
		super();
		this.context = context;
		this.list = items;
	}

	public void setListener(IExpandableAdapter callback) {
		this.callback = callback;
	}

	private List<ModeItem> MillList() {
		List<ModeItem> items = new ArrayList<ModeItem>();
		items.add(getModeItem("美赞成安婴儿A+(2-5段)", 5, 40));
		items.add(getModeItem("雅培金装小安素900g", 5, 37));
		items.add(getModeItem("贝因美金装爱+新生儿配方奶", 5, 50));
		items.add(getModeItem("澳优能力多婴儿奶粉", 5, 55));
		items.add(getModeItem("惠氏金装爱儿乐奶粉", 5, 70));
		items.add(getModeItem("明治珍爱儿奶粉", 5, 70));
		items.add(getModeItem("能恩金盾一阶段婴儿配方奶粉", 5, 50));
		items.add(getModeItem("亨氏超金康儿高婴儿配方奶粉", 5, 45));
		items.add(getModeItem("多美滋锦缎金盾贝护婴儿配方奶粉", 5, 40));
		items.add(getModeItem("百立乐金装婴儿配方奶粉", 5, 50));
		items.add(getModeItem("karicare羊奶粉", 5, 50));
		items.add(getModeItem("Karicare牛奶粉", 5, 70));
		items.add(getModeItem("新西兰爱他美", 5, 70));
		items.add(getModeItem("港版雅培心美力奶粉", 5, 40));
		items.add(getModeItem("日本固力果婴儿奶粉", 5, 70));
		items.add(getModeItem("日本森永婴儿奶粉", 5, 70));
		items.add(getModeItem("港版牛栏婴儿奶粉", 5, 50));
		items.add(getModeItem("荷兰牛栏婴儿奶粉", 5, 37));
		items.add(getModeItem("港版美素佳儿奶粉", 5, 50));
		items.add(getModeItem("荷兰本土美素奶粉", 5, 50));
		return items;
	}

	private List<ModeItem> TeaList() {
		List<ModeItem> items = new ArrayList<ModeItem>();
		String[] teas = context.getResources()
				.getStringArray(R.array.tea_modes);
		String[] values = context.getResources().getStringArray(
				R.array.tea_modes_value);

		for (int i = 0; i < teas.length; i++) {
			items.add(getTeadItem(teas[i], Integer.valueOf(values[i])));
		}

		return items;
	}

	private List<ModeItem> DouruList() {
		List<ModeItem> items = new ArrayList<ModeItem>();
		String[] name = context.getResources().getStringArray(
				R.array.douru_modes);
		String[] values = context.getResources().getStringArray(
				R.array.douru_modes_value);

		for (int i = 0; i < name.length; i++) {
			items.add(getModeItem(name[i], 0, Integer.valueOf(values[i])));
		}

		return items;
	}

	private List<ModeItem> getCoffee() {
		List<ModeItem> items = new ArrayList<ModeItem>();
		// String[] name =
		// context.getResources().getStringArray(R.array.coffee_modes);
		// String[] values =
		// context.getResources().getStringArray(R.array.coffee_modes_value);

		// for(int i=0;i<name.length;i++){
		// items.add(getModeItem(name[i], 0, Integer.valueOf(values[i])));
		// }
		//
		return items;
	}

	private ModeItem getTeadItem(String name, int targetTemp) {
		return getModeItem(name, 0, targetTemp);
	}

	private ModeItem getModeItem(String name, int lv, int targetTemp) {
		ModeItem item = new ModeItem();
		item.setModeTitle(name);
		item.setCleanlvTime(lv);
		item.setTargetTemperature(targetTemp);
		item.setNeedBoiled(true);
		item.setNeedCleanlv(lv > 0 ? true : false);
		return item;
	}

	@Override
	public int getGroupCount() {
		return generals.length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return generals[groupPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		switch (groupPosition) {
		case 0:
			return list.size();
		case 1:
			return MillList().size();
		case 2:
			return TeaList().size();
		case 3:
			return DouruList().size();
		case 4:
			return getCoffee().size();
		}
		return 0;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		switch (groupPosition) {
		case 0:
			return list.get(childPosition).getModeTitle();
		case 1:
			return MillList().get(childPosition).getModeTitle();
		case 2:
			return TeaList().get(childPosition).getModeTitle();
		case 3:
			return DouruList().get(childPosition).getModeTitle();
		case 4:
			return getCoffee().get(childPosition).getModeTitle();
		}
		return "";
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = View.inflate(context, R.layout.group_list2, null);
		ImageView im = (ImageView) view.findViewById(R.id.image);
		TextView tv = (TextView) view.findViewById(R.id.list_name);
		tv.setText(generals[groupPosition]);
		im.setImageResource(logos[groupPosition]);
		return view;
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		View view = View.inflate(context, R.layout.child_list, null);
		TextView info = (TextView) view.findViewById(R.id.info);
		info.setText(getChild(groupPosition, childPosition).toString());
		return view;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}